package comframe.jdbc.sql.support.struct;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import comframe.jdbc.annotation.Generator;
import comframe.jdbc.annotation.Id;
import comframe.jdbc.annotation.Table;
import comframe.jdbc.sql.convert.ConvertUtil;


public class RM {

	private String sql;
	private List<Object> params;
	private Class<?> clazz;
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	public List<Object> getParams() {
		return params;
	}
	public void setParams(List<Object> params) {
		this.params = params;
	}
	
	
	public Class<?> getClazz() {
		return clazz;
	}
	public void setClazz(Class<?> clazz) {
		this.clazz = clazz;
	}
	/****
	 * 
	 * @param object
	 * @return
	 */
	public static List<Object> getObjectParams(Object object) {
		List<Object> list = new ArrayList<Object>();
		Class<? extends Object> clazz = object.getClass();
		// 判断是否是table 注解了
			if (clazz.isAnnotationPresent(Table.class)) {
				getObjectParamsAndSuper(object,clazz,list);
			}
		return list;
	}
	
	private static void getObjectParamsAndSuper(Object object, Class<? extends Object> clazz2, List<Object> list) {
		for (Field field : clazz2.getDeclaredFields()) {
			// 字段名称
			String   filedName = field.getName();
			// 名称
			boolean isSeqName = false;
			boolean isfield = false;
			// 获得字段 注解对象
			Annotation[] ans = field.getAnnotations();
			// 遍历注解对象
			for (Annotation an : ans) {
				 if(Generator.class == an.annotationType()){
					 isSeqName = true;
				}else  if(comframe.jdbc.annotation.Field.class == an.annotationType()){
					isfield = true;
				}else  if(comframe.jdbc.annotation.Id.class == an.annotationType()){
					isfield = true;
				}
			}
			if(!isSeqName&&isfield){
				list.add(ConvertUtil.getProperty(object, filedName,field.getType()));
			}
			isSeqName = false;
			isfield = false;
		}
		
		if(clazz2.getSuperclass() != null){
			getObjectParamsAndSuper(object, clazz2.getSuperclass(), list);
		}
	}
	/****
	 * 
	 * @param object
	 * @return
	 */
	public static List<Object> getObjectParamsHasKey(Object object) {
		List<Object> list = new ArrayList<Object>();
		Class<? extends Object> clazz = object.getClass();
		// 判断是否是table 注解了
		if (clazz.isAnnotationPresent(Table.class)) {
			 getObjectParamsHasKeyAndSuper(object,clazz,list);
		}
		return list;
	}
	
	private static void getObjectParamsHasKeyAndSuper(Object object,
			Class<? extends Object> clazz2, List<Object> list) {
		for (Field field : clazz2.getDeclaredFields()) {
			// 字段名称
			String   filedName = field.getName();
			boolean isfield = false;
			// 获得字段 注解对象
			Annotation[] ans = field.getAnnotations();
			// 遍历注解对象
			for (Annotation an : ans) {
				  if(comframe.jdbc.annotation.Field.class == an.annotationType()){
					isfield = true;
				}else  if(comframe.jdbc.annotation.Id.class == an.annotationType()){
					isfield = true;
				}
			}
			if(isfield){
				list.add(ConvertUtil.getProperty(object, filedName,field.getType()));
			}
			isfield = false;
		}
		if(clazz2.getSuperclass() != null){
			getObjectParamsHasKeyAndSuper(object, clazz2.getSuperclass(), list);
		}
	}
	/****
	 * 
	 * @param object
	 * @return
	 */
	public static List<Object> getObjectParamsHasKeyAndValue(Object object) {
		List<Object> list = new ArrayList<Object>();
		Class<? extends Object> clazz = object.getClass();
		// 判断是否是table 注解了
		if (clazz.isAnnotationPresent(Table.class)) {
		   getObjectParamsHasKeyAndValueAndSuper(object,clazz,list);
		}
		return list;
	}
	
	private static void getObjectParamsHasKeyAndValueAndSuper(Object object,
			Class<? extends Object> clazz2, List<Object> list) {
		for (Field field : clazz2.getDeclaredFields()) {
			// 字段名称
			String   filedName = field.getName();
			boolean isfield = false;
			// 获得字段 注解对象
			Annotation[] ans = field.getAnnotations();
			// 遍历注解对象
			for (Annotation an : ans) {
				  if(comframe.jdbc.annotation.Field.class == an.annotationType()){
					isfield = true;
				}else  if(comframe.jdbc.annotation.Id.class == an.annotationType()){
					isfield = true;
				}
			}
			if(isfield){
				Object obj = ConvertUtil.getProperty(object, filedName,field.getType());
				if(obj != null)
				//插入对象值
				  list.add(obj);
			}
			
		}
		if(clazz2.getSuperclass() != null){
			getObjectParamsHasKeyAndValueAndSuper(object, clazz2.getSuperclass(), list);
		}
	}
	
	public static List<Object> getObjectParams(Object object, String key) {
		List<Object> list = new ArrayList<Object>();
		//插入对象值
		list.add(ConvertUtil.getProperty(object, key));
		return list;
	}
	
 
}
